package cn.appsys.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;

import cn.appsys.biz.CategoryBiz;
import cn.appsys.biz.DictionaryBiz;
import cn.appsys.biz.InfoBiz;
import cn.appsys.biz.ShowInfoBiz;
import cn.appsys.entity.AppCategory;
import cn.appsys.entity.DataDictionary;
import cn.appsys.entity.ShowInfo;

@Controller
public class ShowInfoController {
	@Autowired
	private InfoBiz infoBiz;
	@Autowired
	private ShowInfoBiz showInfoBiz;
	
	@Autowired
	private DictionaryBiz dictionaryBiz;
	
	@Autowired
	private CategoryBiz categoryBiz;
	
	@RequestMapping("/AppInfo")
	public String AppInfo(Model model,String softwareName,Integer status,Integer flatformId,Integer oneselect,
			Integer towselect,Integer threeselect,Integer page){
		page=page==null?1:page;
		status=status==null?-1:status;
		oneselect=oneselect==null?-1:oneselect;
		towselect=towselect==null?-1:towselect;
		threeselect=threeselect==null?-1:threeselect;
		flatformId=flatformId==null?-1:flatformId;
    	int pageSize=4;
    	int startIndex=(page-1)*pageSize;
		List<ShowInfo> showInfoList=showInfoBiz.showInfo(null,status,flatformId, oneselect, towselect, threeselect, startIndex, pageSize);
		int rows=showInfoBiz.getRowsCount(softwareName, status, flatformId, oneselect, towselect, threeselect);
    	int pageCount=(int)Math.ceil((double)rows/pageSize);
		List<DataDictionary> statusList=dictionaryBiz.getByStatus();
		
		List<DataDictionary> flatformList=dictionaryBiz.getByFlatform();
		
		List<AppCategory> listone =categoryBiz.getAllCategory();
		
		model.addAttribute("listone",listone);
		model.addAttribute("flatformList", flatformList);
		model.addAttribute("statusList", statusList);
		model.addAttribute("showInfoList", showInfoList);
		model.addAttribute("totalPages",pageCount);
		model.addAttribute("page",page);
		return "appmaintain";
	}
	@RequestMapping(value="jsonAppInfo",produces = "application/json; charset=utf-8")
	@ResponseBody
	public String jsonAppInfo(String softwareName,Integer status,Integer flatformId,Integer oneselect,Integer towselect,Integer threeselect,Integer page){
		page=page==null?1:page;
		status=status==null?-1:status;
		flatformId=flatformId==null?-1:flatformId;
		oneselect=oneselect==null?-1:oneselect;
		towselect=towselect==null?-1:towselect;
		threeselect=threeselect==null?-1:threeselect;
		int pageSize=4;
    	int startIndex=(page-1)*pageSize;
		
		List<ShowInfo> showInfoList=showInfoBiz.showInfo(softwareName, status, flatformId, oneselect, towselect, threeselect, startIndex, pageSize);
		
		return JSON.toJSONString(showInfoList);
	}
	
	@RequestMapping("/add")
	public String showadd(Model model){
		List<AppCategory> listone =categoryBiz.getAllCategory();
		List<DataDictionary> flatformList=dictionaryBiz.getByFlatform();
		model.addAttribute("flatformList", flatformList);
		model.addAttribute("listone",listone);
		return "addandupdate";
	}
	@RequestMapping("/del")
	public String del(int id ){
		infoBiz.delete(id);
		return "redirect:/AppInfo";
	}
	@RequestMapping("/updatestatus")
	public String updatestatus(int id,int status){
		
		infoBiz.updateStatus(id, status);
		return "redirect:/AppInfo";
	}
	@RequestMapping(value="twoAll",produces={"application/json;charset=utf-8"})
	@ResponseBody
	public String  twoAll(Integer parentId) throws Exception{
		
		return JSON.toJSONString(categoryBiz.getAppCategoryListByParentId(parentId));
	}
	
	@RequestMapping(value="threeAll",produces={"application/json;charset=utf-8"})
	@ResponseBody
	public String threeAll(Integer parentId) throws Exception{
		
		return JSON.toJSONString(categoryBiz.getAppCategoryListByParentId(parentId));
	}
	
	
}
